A database transaction is a unit of interaction with a database management system (DBMS) or similar system that is treated in a coherent and reliable way independent of other transactions that must be either entirely completed or aborted. In database products, the ability to handle database transactions allows the user to ensure that integrity of a database is maintained.
A single database transaction may require several queries, each reading and/or writing information in the database. When the queries are executed, updates to the database are not visible to the outside world. When the database transaction is completed, a commit statement is executed, making the updates visible to the outside world. If one of the queries fails, the database system may rollback either the entire transaction or just the failed query. The transaction can also be rolled back manually at any time before the commit.
One of the key DBMS features is known as atomicity. Atomicity refers to the ability of the DBMS to guarantee that either all of the operations of a transaction are performed or none of them are. For example, the transfer of funds can be completed or it can fail for a multitude of reasons, but atomicity guarantees that one account will not be debited if the other is not credited as well. Typically, the DBMS uses locking to provide atomicity. In particular, the DBMS ensures that a lock is acquired anytime before processing data in a database, even on read operations. Maintaining a large number of locks, however, results in substantial overhead as well as hurting concurrency. If user A is running a transaction that has read a row of data that user B wants to modify, for example, user B must wait until user A's transaction is finished. In addition, database transactions can neither persist across session boundaries nor survive application server and database server restarts.